<main class="" th:fragment="deliveryAdmin">
    <body>

    <table id="dg" title="出库管理" class="easyui-datagrid" style="width:1280px;height:500px"
           url="/delivery/s"
           toolbar="#toolbar" pagination="true"
           rownumbers="true" fitColumns="true" singleSelect="true">
        <thead>
        <tr>
            <th field="id" width="50">出库ID</th>
            <th field="commodityName" width="50">商品名称</th>
            <th field="num" width="50">出库数</th>
            <th field="saveUser" width="50">出库人</th>
            <th field="remarks" width="50">备注</th>
            <th field="examineUser" width="50">审核人</th>
            <th field="examineState" width="50">审核状态</th>
            <th field="examineTime" width="50">审核时间</th>
        </tr>
        </thead>
    </table>
    <div id="toolbar">
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newdelivery()">添加出库</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="destroydelivery()">删除出库信息</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="examine()">审核出库信息</a>
        <div id="tb" style="padding:3px">
            <span>商品ID:</span>
            <input id="remarks" style="line-height:26px;border:1px solid #ccc">
            <span>商品昵称:</span>
            <input id="commodityName" style="line-height:26px;border:1px solid #ccc">

            <a href="#" class="easyui-linkbutton" plain="true" onclick="doSearch()">搜索</a>
        </div>
    </div>

    <div id="dlg" class="easyui-dialog" style="width:550px"
         data-options="closed:true,modal:true,border:'thin',buttons:'#dlg-buttons'">
        <form id="fm" method="post" novalidate style="margin:0;padding:20px 50px">
            <h3>出库信息</h3>
            <div style="margin-bottom:10px">
                <input class="easyui-combobox" name="commodityId" label="商品信息:" style="width:100%"
                       data-options="
                      valueField:'id',
                      textField:'name',
                      url:'/commodity/select',
                      onSelect: function(rec){
                            var url = rec.stock;
                            $('#cc-stock').numberbox('setValue',url);
                            }">
            </div>
            <div style="margin-bottom:10px">
                <input  id="cc-stock" class="easyui-numberbox" readonly="readonly"  label="库存数:" style="width:100%">
            </div>
            <div style="margin-bottom:10px">
                <input id="cc-outNum" name="num" class="easyui-numberbox" required="true"  label="出库数:" style="width:100%">
            </div>
            <div style="margin-bottom:10px">
                <input name="remarks" class="easyui-textbox" required="true" label="备注:" style="width:100%">
            </div>

        </form>
    </div>
    <div id="dlg-buttons">
        <a href="javascript:void(0)" class="easyui-linkbutton c6" iconCls="icon-ok" onclick="savedelivery()"
           style="width:90px">Save</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel"
           onclick="javascript:$('#dlg').dialog('close')" style="width:90px">Cancel</a>
    </div>
    <script type="text/javascript">
        var url;

        //搜索
        function doSearch() {
            var data = {};
            if ($("#commodityName").val()) {
                data.commodityName = $("#commodityName").val();
            }
            if ($('#remarks').val()) {
                data.id = $('#remarks').val();
            }
            $('#dg').datagrid('load', data);
        }

        //添加
        function newdelivery() {
            $('#dlg').dialog('open').dialog('center').dialog('setTitle', '新增出库');
            $('#fm').form('clear');
            url = '/delivery/add';
        }

        //修改
        function editdelivery() {
            var row = $('#dg').datagrid('getSelected');
            if (row) {
                $('#dlg').dialog('open').dialog('center').dialog('setTitle', '修改出库');
                $('#fm').form('load', row);
                url = '/delivery/up?id=' + row.id;
            }
        }

        //添加和修改保存出库
        function savedelivery() {
            $('#fm').form('submit', {
                url: url,
                onSubmit: function () {
                    return $(this).form('validate');
                },
                success: function (result) {
                    if (result == 'false-has-double') {
                        $.messager.show({
                            title: 'Error',
                            msg: '重名'
                        });
                    } else if (result == 'LessThanInventory') {
                        $.messager.show({
                            title: 'Error',
                            msg: '出库量大于实际库存'
                        });
                    } else {
                        var result = eval('(' + result + ')');
                        $('#dlg').dialog('close');        // close the dialog
                        $('#dg').datagrid('reload');    // reload the delivery data
                    }
                }
            });
        }

        //删除
        function destroydelivery() {
            var row = $('#dg').datagrid('getSelected');
            if (row) {
                $.messager.confirm('Confirm', '确定要销毁此出库吗?', function (r) {
                    if (r) {
                        $.post('/commodity/select', {id: row.id}, function (result) {
                            if(result){
                                $.messager.show({    // show error message
                                    title: 'Error',
                                    msg: "不允许删除，当前入库下存在商品"
                                });
                            }else{
                                $.post('/delivery/del', {id: row.id}, function (result) {
                                    if (result) {
                                        $('#dg').datagrid('reload');    // reload the delivery data
                                    } else {
                                        $.messager.show({    // show error message
                                            title: 'Error',
                                            msg: "系统异常"
                                        });
                                    }
                                }, 'json');
                            }
                        }, 'json');
                    }
                });
            }
        }

        //审核
        function examine() {
            var row = $('#dg').datagrid('getSelected');
            if(row.examineState != '待审核'){
                alert('当前商品不允许进行审核');
                return ;
            }
            if(row.saveUser == $("#usermeesage").val()){
                alert('不允许对自己的请求进行审核');
                return ;
            }
            if (row) {

                $.messager.confirm('Confirm', 'OK为通过审核,Cancel为未通过审核', function (r) {
                    if (r) {
                        $.post('/delivery/examineUp', {id:row.id,commodityId:row.commodityId,examineState:"通过审核",num:row.num}, function (result) {
                            if (result) {
                                if (result == 'LessThanInventory') {
                                    $.messager.show({
                                        title: 'Error',
                                        msg: '出库量大于实际库存,库存不足'
                                    });
                                }
                                $('#dg').datagrid('reload');    // reload the warehousing data
                            }
                        }, 'json');
                    }else{
                        $.post('/delivery/examineUp', {id:row.id,commodityId:row.commodityId,examineState:"未通过审核",num:row.num}, function (result) {
                            if (result) {
                                if (result == 'LessThanInventory') {
                                    $.messager.show({
                                        title: 'Error',
                                        msg: '出库量大于实际库存,库存不足'
                                    });
                                }
                                $('#dg').datagrid('reload');    // reload the warehousing data
                            }
                        }, 'json');
                    }

                });
            }
        }
    </script>
    </body>
</main>
